package com.design.testdemo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.design.testdemo.R;
import com.design.testdemo.TimerUtils;

public class TimerActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTvTime;
    private Button mBtnStart;
    private Button mBtnPause;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);

        mTvTime = (TextView) findViewById(R.id.tv_time);
        mBtnStart = (Button) findViewById(R.id.start);
        mBtnPause = (Button) findViewById(R.id.pause);
        mBtnStart.setOnClickListener(this);
        mBtnPause.setOnClickListener(this);


        TimerUtils.getInstance().setTimeListener(new TimerUtils.TimeListener() {
            @Override
            public void onTimeChange(long totalCurrTime, long deputyCurrTime) {

                // totalCurrTime总的时间
                // deputyCurrTime 副的时间

                mTvTime.setText(getFormatTime(totalCurrTime) + "\n" + (getFormatTime(deputyCurrTime)));
            }
        });

        mTvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimerUtils.getInstance().initTime();
            }
        });

    }

    private String getFormatTime(long time) {

        long millisecond = time % 1000;
        long second = (time / 1000) % 60;
        long minute = time / 1000 / 60;

        //秒以下的只显示一位
        String strMillisecond = millisecond / 10 + "";

        if (strMillisecond.length() == 1) {
            strMillisecond = "0" + strMillisecond;
        }

        //秒显示两位
        String strSecond = ("00" + second).substring(("00" + second).length() - 2);
        //分显示两位
        String strMinute = ("00" + minute).substring(("00" + minute).length() - 2);

        return strMinute + ":" + strSecond + "." + strMillisecond;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                // 开始
                TimerUtils.getInstance().setPause_totalTime(false);
                TimerUtils.getInstance().setPause_deputyTime(false);
                break;
            case R.id.pause:
                // 暂停
                TimerUtils.getInstance().setPause_totalTime(true);
                TimerUtils.getInstance().setPause_deputyTime(true);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        TimerUtils.getInstance().stopTimer();

    }
}
